const express = require('express');
const router = express.Router();
const db = require('../config/db');
function checkLogin(req, res, next) {
    if (req.session.user) {
        next();
    } else {
        res.redirect('/auth/login');
    }
}
router.get('/get-user-products', checkLogin, (req, res) => {
    const userId = req.session.user.id;
    db.query('SELECT * FROM products WHERE user_id = ?', [userId], (err, results) => {
        if (err) throw err;
        res.json(results);
    });
});
router.post('/add-product', checkLogin, (req, res) => {
    const userId = req.session.user.id;
    const { name, price, description } = req.body;
    db.query('INSERT INTO products (user_id, name, price, description) VALUES (?, ?, ?, ?)', [userId, name, price, description], (err, result) => {
        if (err) throw err;
        res.redirect('/user/manage');
    });
});

module.exports = router;